# coding=UTF-8
import os
import glob
from JTK.jcore.JFile.JJson import JJson
from .ShelfSetup import ShelfSetup


def auto_shelf():
    current_path = os.path.dirname(__file__)

    # config_path = os.path.join(current_path, "config.json")
    config_path_list = glob.glob('%s/config/*.json' % current_path)
    for config_path in config_path_list:
        # print config_path
        config_data = JJson(config_path).read()
        for proj in config_data:
            try:
                shelf = ShelfSetup(name=proj)
            except:
                return
                # log.info(proj)
            item_list = config_data[proj]
            # log.info(item_list)
            for item in item_list:
                # log.info(item)
                if item["name"] == "addSeparator":
                    shelf.addSeparator()
                else:
                    shelf.addButon(label=item["name"], icon=item["icon"], annotation=item["ann"],
                                   command=item["command"])
